 package com.fizz.redpacket.algorithm;


 import com.fizz.redpacket.algorithm.AllocateStrategy;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Random;


 public class MyAllocateStrategy
   implements AllocateStrategy
 {
   public List<String> allocate(int count, String moneyStr) {
     double moneyDouble;
     if (count <= 0) {
       throw new RuntimeException("参数错误");
     }


     try {
       moneyDouble = Double.parseDouble(moneyStr);
     } catch (Exception ex) {
       throw new RuntimeException("参数错误 money = " + moneyStr);
     }

     int money = (int)(moneyDouble * 100.0D);
     if (money < count) {
       throw new RuntimeException("金额必须大于等于 " + count + "分");
     }


     Random random = new Random();
     List<String> res = new ArrayList<>();
     int sum = money;
     int personCount = count;



     while (personCount > 1) {
       int randomMoney, range = sum / personCount;
       if (range == 1) {
         randomMoney = 1;
       } else {
         randomMoney = random.nextInt(range - 1) + 1;
       }
       personCount--;
       sum -= randomMoney;

       res.add(String.valueOf(randomMoney * 1.0D / 100.0D));
     }
     res.add(String.valueOf(sum * 1.0D / 100.0D));

     return res;
   }
 }
